﻿using Digitalmes.Application.Sys.Orgs;
using Digitalmes.Application.Sys.Orgs.Commands;
using Digitalmes.Application.Sys.Orgs.Queries;

namespace Digitalmes.WebApi.Endpoints;

/// <summary>
/// 组织机构管理
/// </summary>
public sealed class OrgEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this)
            .RequireAuthorization()
            .MapGet(GetOrgListAsync, "list")
            .MapPost(CreateOrgAsync)
            .MapPut(UpdateOrgAsync)
            .MapDelete(DeleteOrgAsync, "{id}");
    }

    public async Task<IApiResult> GetOrgListAsync(ISender sender, [AsParameters] GetOrgQuery query)
    {
        var result = await sender.Send(query);
        return ApiResult.FromResult(result);
    }

    public async Task<IApiResult> CreateOrgAsync(ISender sender, SysOrgDto dto)
    {
        var result = await sender.Send(new CreateOrgCommand(dto));
        return ApiResult.FromResult(result);
    }

    public async Task<IApiResult> UpdateOrgAsync(ISender sender, SysOrgDto dto)
    {
        var result = await sender.Send(new UpdateOrgCommand(dto));
        return ApiResult.FromResult(result);
    }

    public async Task<IApiResult> DeleteOrgAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteOrgCommand(id));
        return ApiResult.FromResult(result);
    }
}
